import { App } from 'vue';
import { useUserStore } from '../../stores/UserStore';

export default {
  install(app: App) {
    app.directive('permission', {
      mounted(el, binding) {
        const userStore = useUserStore();
        const requiredRoles = binding.value as string[];

        if (requiredRoles && requiredRoles.length > 0) {
            const premissioms = userStore.userInfo.authorityStrings || [];
          const hasPermission = requiredRoles.some((role) =>
            premissioms.indexOf(role) >= 0
          );
          if (!hasPermission) {
            el.parentNode && el.parentNode.removeChild(el);
          }
        }
      },
    });
  },
};